package com.thssh.ui.other.service.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.thssh.ui.base.BaseActivity;
import com.thssh.ui.other.service.BindService;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/10/11
 * email: zhangyugehu@163.com
 */

public abstract class BaseBindActivity extends BaseActivity {
    private String mTag = getClass().getName();

    class BaseServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(mTag, "onServiceConnected: ");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(mTag, "onServiceDisconnected: ");
        }
    }

    private ServiceConnection conn;
    private boolean isBindService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isBindService = setBindService();
        if(isBindService) {
            Intent intent = new Intent(this, BindService.class);
            conn = new BaseServiceConnection();
            bindService(intent, conn, Context.BIND_AUTO_CREATE);
        }
    }

    protected abstract boolean setBindService();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
